/**
 * Created by LENOVO on 2016/8/31.
 * 用于处理workDetail.jsp中事件的处理
 */
var contextPath = "/";
$(function () {
    contextPath += window.location.pathname.split("/")[1];
    $("#toptitle").html("作业明细表");
    $.ajaxSetup({
        async:false
    });
    var url = contextPath+"/teacherWorkController/getAllwork.wm";
    var jobTaskId = $("#jobTaskId").text();
    data = {};
    data.jobTaskId = jobTaskId;
    $.post(url,data,function (result) {
        var newrow = "";
        var newrow1 = "";
        var newrow2 = "";
        var sharenewrow = "";
        var imgnewrow = "";
        for(var i=0;i<result.all.length;i++){
            var ss = result.all[i].work_date.split(".");
            var reportTime = ss[0];
            var name = result.all[i].aStudent.student_name;
            var student = result.all[i].aStudent.student_id;
            var good = result.all[i].work_goodjob;
            var workId = result.all[i].work_id;
            var work = result.all[i].work_title;
            newrow += "<tr class='tlength' style='display: table-row;opacity: 1'>" ;
            if(good == "否"){
                imgnewrow = "<td><img src='"+contextPath+"/media/img/teacherwork/sad.jpg' id='img"+workId+"'/ ></td>";
            }
            if(good == "是"){
                imgnewrow = "<td><img src='"+contextPath+"/media/img/teacherwork/happy.jpg' id='img"+workId+"'/ ></td>";
            }
            newrow1 = "<td>"+reportTime+"</td><td>"+name+"</td><td>"+work+"</td>";
                if(good == "否"){
                  sharenewrow="<td><a id='share"+workId+"' class='share' onclick='share()'  href='javascript:void(0);' studentId='"+student+"' workId='"+workId+"' good='"+good+"'>点我分享</a>"
                }
                if(good == "是") {
                    sharenewrow= "<td><a id='cancel"+workId+"' class='cancel' onclick='cancel()' href='javascript:void(0);' studentId='" + student + "' workId='" + workId + "' good='" + good + "'>取消分享</a>"
                }
            newrow2 ="<a class='download' href='"+contextPath+"/teacherWorkController/download.wm?workId="+workId+"'>下载附件</a>" +
                "<a href='"+contextPath+"/teacherWorkController/preview.wm?workId="+workId+"&jobTaskId="+jobTaskId+"'>评价</a></td></tr>";
            newrow=newrow+imgnewrow+newrow1+sharenewrow+newrow2;
        }
        $("#datas tbody").html(newrow);
        $("#total").text($(".tlength").length);
        $("#totalReport").text($(".tlength").length);
    },"json");
    share();
    cancel();
    $("#table_demo3 .holder").jPages({
        containerID : "data",
        previous : "«",
        next : "»",
        perPage : 6,
        delay : 10,
        callback: function(pages, items){
            $('#total').text(items.count);
            $('#cur_page').text(pages.current);
            $('#total_page').text(pages.count);
        }
    });
});
function share() {
    $(".share").click(function () {
        var url =  contextPath+"/teacherWorkController/share.wm";
        var data = {};
        data.studentId=$(this).attr("studentId");
        data.workId=$(this).attr("workId");
        data.good="是";
        var thiselm = $(this);
        $.post(url,data,function (result) {
            if(result.msg == "true"){
                $("#img"+result.id).attr("src",contextPath+"/media/img/teacherwork/happy.jpg");
                thiselm.text("取消分享");
                thiselm.removeClass("share").addClass("cancel");
                thiselm.unbind("click");
                 cancel();
            }else {
                alert("分享失败！");
            }
        },"json");
    });
}
function cancel() {
    $(".cancel").click(function () {
        var url =  contextPath+"/teacherWorkController/share.wm";
        var data = {};
        data.studentId=$(this).attr("studentId");
        data.workId=$(this).attr("workId");
        data.good="否";        //将数据库中的改为否
        var thiselm = $(this);
        var imgelm = $(".img");
        $.post(url,data,function (result) {
            if(result.msg == "true"){
                $("#img"+result.id).attr("src",contextPath+"/media/img/teacherwork/sad.jpg");
                 thiselm.text("点我分享");
                 thiselm.removeClass("cancel").addClass("share");
                 thiselm.unbind("click");
                share();
            } else{
                alert("取消分享失败！");
            }
        },"json");
    });
}

    

